/* 
 * Add listeners and populate search field with the search request
 */
$(document).ready(function () {                                 
    $('#searchBtn').live('click',function(){search();});        
    $('#searchName').live('keypress',function(field,e){wasEnter(field,e)});     
    $('#searchTitle').live('keypress',function(field,e){wasEnter(field,e)});
    var addres = window.location.href;
    addres = addres.split('?')[1];
    var name = "";
    var title = "";
    var typesearch = false;
    jQuery.each(addres.split('&'), function(i,attrebut){            
        attrebut = attrebut.split('=');
        var property = attrebut[0];
        var value = attrebut[1];
        value = value.replace("%20", " ");
        if(property == "name"){
            name = value;
        }
        if(property == "title"){
            title = value;
        }
        if(property == "type"){
            if(value == "search"){
                typesearch = true;
            }
        }
    });
    if(typesearch) {
        $('#searchTitle').attr("value",title);
        $('#searchName').attr("value", name );
    }
    
});

/**
 * Make search on enter press. (Do not work in firefox). 
 */

function wasEnter(field,e)                  //
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   search();
   }

}

/**
 *  Retrieve value in search field, and forward to search page.
 */

function search() {
    var name = $("#searchName").attr("value");
    var title = $("#searchTitle").attr("value");
    var addreas = "DisplayServlet?type=search";
    if( name != "" ){
        addreas = addreas + "&name="+name;
    }
    if( title != "" ){
        addreas = addreas + "&title="+title;
    }
    window.location.href=addreas;
   
}
